
package com.probiz.estoresf.customer.web.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.probiz.estore.common.helper.ConfigUtil;
import com.probiz.estore.common.model.customer.ValidationSession;
import com.probiz.estore.common.util.DateUtil;
import com.probiz.estore.core.controller.BaseController;
import com.probiz.estore.customer.service.ValidationSessionManager;
import com.probiz.estore.system.service.MsgSendManager;
import com.probiz.estore.system.util.MailEngine;
import com.probiz.estore.system.util.MsgTemplateConstants;
import com.probiz.estore.webapp.util.RequestContext;

public class PasswordRecoverFrontController extends BaseController {

	private ValidationSessionManager	validationSessionManager	= null;
	protected MailEngine	mailEngine					= null;
	private MsgSendManager msgSendManager = null;
	
	
	public void setMsgSendManager(MsgSendManager msgSendManager) {
		this.msgSendManager = msgSendManager;
	}

	public ModelAndView defaultAction(HttpServletRequest request,
			HttpServletResponse response) {
		return new ModelAndView("/customer/forgetPassword");
	}

	public ModelAndView requestEmailAction(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("enter PasswordRecoverFrontController requestEmailAction method...");
		}
		String email = request.getParameter("email");

		//
		ValidationSession vs = validationSessionManager.doGenerateUrlByEmail(
				email, ValidationSession.TYPE_PASSWORD_RECOVER);
		String message = "";

		if (vs != null) {
			String domain = ConfigUtil.getInstance().getStoreFrontSiteUrl();
			// construct the full url.
			String fullUrl = domain + "/customer/resetPassword.html?uid="
					+ vs.getUrl();

			Map map = new HashMap();
			map.put("email", vs.getEmail());
			map.put("url", fullUrl);
			map.put("expiredDate",DateUtil.convertDateToString(vs.getExpiredDate(),ConfigUtil.getInstance().getDateTimePattern()));
			map.put("ip", request.getRemoteAddr());
			map.put("domain", domain);
			map.put("companInfo", RequestContext.getServletContext().getAttribute("companyInfo"));

			// send email

			//mailEngine.sendSimpleTemplateMail("passwordRecover.vm", map, emailTitle, from, email);
			msgSendManager.sendMsg(MsgTemplateConstants.MESSAGE_FOTGET_PASSWORD, map, new String[]{email}, null, null);
			message = getMessage("passwordRecover.email.emailSuccess",
					new Object[] { email });
			saveMessage(request, message);
		} else {
			message = getMessage("passwordRecover.error.emailWrong",
					new Object[] { email });
			//request.setAttribute("errors", message);
			saveErrorMessage(request, message);
		}
		return new ModelAndView("/customer/forgetPassword");
	}

	public void setValidationSessionManager(
			ValidationSessionManager validationSessionManager) {
		this.validationSessionManager = validationSessionManager;
	}
	
	
    public ModelAndView testkk(HttpServletRequest request,HttpServletResponse response){ 
//    	mailEngine.sendSimpleTemplateMail("activateUser.html",new HashMap(),"aaa",from,customer.getEmail());
    	 Map model = new HashMap();
         model.put("message",
                 "If you can read this message, it means it really work now.");
         mailEngine.sendSimpleTemplateMail("activateUser.html", model,
                 null, "ProBIZ@probiz.cn", "kedou@probiz.cn");
    	return null;
    }

	

	@Override
	protected void initController() throws Exception {
		
	}

	public void setMailEngine(MailEngine mailEngine) {
		this.mailEngine = mailEngine;
	}
}
